package com.dp.inspect;


import com.alibaba.fastjson.JSONObject;
import com.dp.enmu.CommonEnum;
import com.dp.io.JSONUtil;
import com.dp.pages.PageHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@Component
@EnableAsync
@RequestMapping("pullInspect")
public class HdCreateWorkTake {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    PageHelp pageHelp;

    private String urlPath = "http://218.26.86.44:9301/doublePre/";

//    @Scheduled(
//            cron = "0 30 0 * * ? "
//    )
//    @PostConstruct
    public void uploadInitMessages() {
        try{
            Map<String, Object> initMessage = jdbcTemplate.queryForList("select token, com_coalcode from basic_company").get(0);
            LocalDate datePust = LocalDate.now().minusDays(1);

//            initMoudlePlan(initMessage, datePust.toString());
//            initMoudleItem(initMessage, datePust.toString());
//            initMoudleScore(initMessage, datePust.toString());

            initMoudlePlan(initMessage, "2024-08-22");
            initMoudlePlan(initMessage, "2024-07-26");


            initMoudleItem(initMessage, "2024-04-28");
            initMoudleItem(initMessage, "2024-05-24");
            initMoudleItem(initMessage, "2024-06-18");

            initMoudleScore(initMessage, "2024-04-28");
            initMoudleScore(initMessage,  "2024-05-24");
            initMoudleScore(initMessage, "2024-06-18");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void initMoudleCompany(Map<String, Object> initMessage, String date) {
        List<Map<String, Object>> list = HttpUrlClient
                .get(urlPath + "zxjc/getCheckCompany",
                        initMessage.get("token").toString(), initMessage.get("com_coalcode").toString(), date);
        rocketSaveCpmpany(list);
    }

    public void initMoudlePlan(Map<String, Object> initMessage, String date) {
        List<Map<String, Object>> list = HttpUrlClient
                .get(urlPath + "zxjc/getCheckTaskCompany",
                        initMessage.get("token").toString(), initMessage.get("com_coalcode").toString(), date);

        rocketSavePlan(list);
    }

    public void initMoudleItem(Map<String, Object> initMessage, String date) {
        List<Map<String, Object>> list = HttpUrlClient
                .get(urlPath + "zxjc/getCheckItemCompany",
                        initMessage.get("token").toString(), initMessage.get("com_coalcode").toString(), date);

        rocketSaveItem(list);
    }

    public void initMoudleScore(Map<String, Object> initMessage, String date) {
        List<Map<String, Object>> list = HttpUrlClient
                .get(urlPath + "zxjc/getCheckScoreCompany",
                        initMessage.get("token").toString(), initMessage.get("com_coalcode").toString(), date);

        rocketSaveScore(list);
    }






    //=========================================接收部级下发，进行处理=====================================================



    public void rocketSaveCpmpany(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list){
            String sql = " insert into inspect_company (id, task_id, company_code, deleted, create_date, create_by, update_date, update_by) SELECT '"+ map.get("id") +"', '"+ map.get("taskId") +"', '"+ map.get("companyCode") +"', '"+ map.get("deleted") +"', '"+ map.get("createDate") +"', '"+ map.get("createBy") +"', '"+ map.get("updateDate") +"', '"+ map.get("updateBy") +"' FROM DUAL WHERE NOT EXISTS ( SELECT 1 FROM inspect_company WHERE id = '"+ map.get("id") +"' ); ";
            jdbcTemplate.execute(sql);

            jdbcTemplate.execute(" INSERT INTO inspect_company (id, task_id) VALUES ('" + map.get("id") + "', '" + map.get("taskId") + "') ON DUPLICATE KEY UPDATE ins_name = '" + map.get("taskName") + "', ins_type = '" + map.get("checkType") + "', ins_starttime = '" + map.get("taskStartTime") + "',ins_endtime = '" + map.get("taskEndTime") + "', ins_describe = '" + map.get("taskContent") + "', ins_type_name = '" + map.get("taskTypeName") + "', item_type = '0', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "', if_hide = '" + map.get("if_hide") + "' ");
        }
    }




    public void rocketSavePlan(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list){
            switch (map.get("checkType").toString()){
                case "1":
                    map.put("checkType","企业自查");
                    map.put("if_hide","1");
                    break;
                case "2":
                    map.put("checkType","市级及以下检查");
                    map.put("if_hide","0");
                    break;
                case "3":
                    map.put("checkType","省级检查");
                    map.put("if_hide","0");
                    break;
                case "4":
                    map.put("checkType","部级检查");
                    map.put("if_hide","0");
                    break;
                default:
                    map.put("checkType","部级检查");
                    map.put("if_hide","0");
            }

            jdbcTemplate.execute(" INSERT INTO inspect_plan (id, ins_id, ins_name, ins_type, ins_starttime, ins_endtime, ins_describe, ins_type_name, item_status, item_type, finish_state, deleted, create_date, create_by, update_date, update_by, if_hide) VALUES ('" + map.get("id") + "', '"+ map.get("taskId") +"', '"+ map.get("taskName") +"','" + map.get("checkType") + "', '" + map.get("taskStartTime") + "', '" + map.get("taskEndTime") + "', '" + map.get("taskContent") + "', '" + map.get("taskTypeName") + "', '0', '0', '1', '" + map.get("deleted") + "', '" + map.get("createDate") + "', '" + map.get("createBy") + "', '" + map.get("updateDate") + "', '" + map.get("updateBy") + "', '"+ map.get("if_hide") +"') ON DUPLICATE KEY UPDATE ins_id = '"+ map.get("taskId") +"', ins_name = '" + map.get("taskName") + "', ins_type = '" + map.get("checkType") + "', ins_starttime = '" + map.get("taskStartTime") + "',ins_endtime = '" + map.get("taskEndTime") + "', ins_describe = '" + map.get("taskContent") + "', ins_type_name = '" + map.get("taskTypeName") + "', item_status ='1', item_type = '0', finish_state = '1', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "', if_hide = '"+ map.get("if_hide") +"' , ONAME = '', OID = '', RISK_UNIT_NAME ='', RISK_UNIT_ID= '' ");


        }
    }



    public void rocketSaveItem(List<Map<String, Object>> list) {
        Map<String, Object> itemIds = new HashMap<>();
        for (Map<String, Object> map : list){
            if(!itemIds.containsKey(map.get("taskId").toString())){
                itemIds.put(map.get("taskId").toString(), map.get("checkItemId"));
            }else {
                itemIds.put(map.get("taskId").toString(), itemIds.get(map.get("taskId")).toString()+ "," + map.get("checkItemId"));
            }

            jdbcTemplate.execute(" INSERT INTO inspect_item (id, item_id, ins_id, check_num, check_content, check_way, check_basis, applicable_place, source_type, deleted, create_date, create_by, update_date, update_by) VALUES ('" + map.get("id") + "', '"+ map.get("checkItemId") +"', '"+ map.get("taskId") +"','" + map.get("checkName") + "', '" + map.get("checkContent") + "', '" + map.get("checkWay") + "', '" + map.get("checkBasis") + "', '" + map.get("applicablePlace") + "', '"+map.get("checkType")+"', '" + map.get("deleted") + "', '" + map.get("createDate") + "', '" + map.get("createBy") + "', '" + map.get("updateDate") + "', '" + map.get("updateBy") + "') ON DUPLICATE KEY UPDATE item_id = '"+ map.get("checkItemId") +"', ins_id = '" + map.get("taskId") + "', check_num = '" + map.get("checkName") + "', check_content = '" + map.get("checkContent") + "',check_way = '" + map.get("checkWay") + "', check_basis = '" + map.get("checkBasis") + "', applicable_place = '" + map.get("applicablePlace") + "', source_type = '"+map.get("checkType")+"', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "' ");

        }
        if(list.size() != 0){
            for (String key : itemIds.keySet()) {
                jdbcTemplate.execute(" update inspect_plan set item_ids = '"+ itemIds.get(key) +"' where ins_id = '"+ key +"' ");
            }
        }
    }



    public void rocketSaveScore(List<Map<String, Object>> list) {
        Map<String, Object> scoreIds = new HashMap<>();
        for (Map<String, Object> map : list){
            if(!scoreIds.containsKey(map.get("taskId").toString())){
                scoreIds.put(map.get("taskId").toString(), map.get("scoreId"));
            }else {
                scoreIds.put(map.get("taskId").toString(), scoreIds.get(map.get("taskId")).toString() +","+map.get("scoreId") );
            }

            jdbcTemplate.execute(" INSERT INTO inspect_score (id, score_id, item_id, ins_id, score_name, is_reject, deduct_point, deduct_illustrate, source_type, deleted, create_date, create_by, update_date, update_by) VALUES ('"+ map.get("id") +"', '"+ map.get("scoreId") +"', '"+ map.get("checkItemId") +"', '"+ map.get("taskId") +"', '政府下发评分细则项', '"+ map.get("isReject") +"', '"+ map.get("deductPoint") +"', '"+ map.get("deductIllustrate") +"', '"+map.get("checkType")+"', '"+ map.get("deleted") +"', '"+ map.get("createDate") +"', '"+ map.get("createBy") +"', '"+ map.get("updateDate") +"', '"+ map.get("updateBy") +"') ON DUPLICATE KEY UPDATE score_id = '"+ map.get("scoreId") +"', item_id = '" + map.get("checkItemId") + "', ins_id = '" + map.get("taskId") + "', score_name = '政府下发评分细则项', is_reject = '" + map.get("isReject") + "', deduct_point = '" + map.get("deductPoint") + "', deduct_illustrate = '" + map.get("deductIllustrate") + "', source_type = '"+map.get("checkType") +"', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "' ");
        }
        if(list.size() != 0){
            for (String key : scoreIds.keySet()) {
                jdbcTemplate.execute(" update inspect_plan set score_ids = '"+ scoreIds.get(key) +"' where ins_id = '"+ key +"' ");
            }
        }
    }




}
